package org.linlinjava.litemall.admin.web2.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req1;
import org.linlinjava.litemall.admin.web2.system.dto.req.Req2;
import org.linlinjava.litemall.admin.web2.system.dto.resp.Resp1;
import org.linlinjava.litemall.core.system.SystemStaticParam;
import org.linlinjava.litemall.core.util.R;
import org.linlinjava.litemall.core.util.Util;
import org.linlinjava.litemall.db.dao.TPrizeMapper;
import org.linlinjava.litemall.db.dao.TPrizeRecordMapper;
import org.linlinjava.litemall.db.domain.TPrize;
import org.linlinjava.litemall.db.domain.TPrizeExample;
import org.linlinjava.litemall.db.domain.TPrizeRecordExample;
import org.linlinjava.litemall.db.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 积分抽奖设置
 */
@Api("系统设置-积分抽奖设置")
@RestController
@RequestMapping("/system/score")
public class SystemScoreController {


    @Autowired
    private RedisUtil redisUtil;


    @ApiOperation(value = "基础设置")
    @GetMapping("/setting")
    public Object setting() {
        Map<String, Object> map = Util.hMap();
        map.put("newUserScore", redisUtil.getInt(SystemStaticParam.newUserScore));
        map.put("costScore", redisUtil.getInt(SystemStaticParam.costScore));
        return R.ok(map);
    }

    @ApiOperation(value = "基础设置保存", notes = "传newUserScore、costScore")
    @PostMapping("/enable")
    public Object enable(@RequestBody Req1 req) {
        redisUtil.set(SystemStaticParam.costScore,req.getCostScore());
        redisUtil.set(SystemStaticParam.newUserScore,req.getNewUserScore());
        return R.ok();
    }

    @ApiOperation(value = "列表数据")
    @GetMapping("/list")
    public Object list() {
        List<TPrize> tPrizes = prizeMapper.selectByExample(new TPrizeExample());
        List<Resp1> res = Util.aList(tPrizes.size());
        tPrizes.forEach(p -> {
            Resp1 dto = new Resp1();
            dto.setId(p.getId());
            dto.setName("奖项" + p.getId());
            switch (p.getType()) {
                case 0:
                    dto.setItem(p.getItem() + "元红包");
                    break;
                case 1:
                    dto.setItem(p.getItem() + "元券");
                    break;
                case 2:
                    dto.setItem(p.getItem() + "积分");
                    break;
            }
            dto.setChance(p.getChance());
            long o30 = prizeRecordMapper.countByExample(TPrizeRecordExample.newAndCreateCriteria().andPIdEqualTo(p.getId()).andActionEqualTo(0)
                    .andCreatedTimeGreaterThanOrEqualTo(LocalDateTime.now().plusDays(-30)).example());
            long u30 = prizeRecordMapper.countByExample(TPrizeRecordExample.newAndCreateCriteria().andPIdEqualTo(p.getId()).andActionEqualTo(1)
                    .andCreatedTimeGreaterThanOrEqualTo(LocalDateTime.now().plusDays(-30)).example());
            dto.setO30(o30);
            dto.setU30(u30);
            dto.setL30((long) (((double) u30 / o30) * 100) + "%");

            long o90 = prizeRecordMapper.countByExample(TPrizeRecordExample.newAndCreateCriteria().andPIdEqualTo(p.getId()).andActionEqualTo(0)
                    .andCreatedTimeGreaterThanOrEqualTo(LocalDateTime.now().plusDays(-90)).example());
            long u90 = prizeRecordMapper.countByExample(TPrizeRecordExample.newAndCreateCriteria().andPIdEqualTo(p.getId()).andActionEqualTo(1)
                    .andCreatedTimeGreaterThanOrEqualTo(LocalDateTime.now().plusDays(-90)).example());
            dto.setO90(o90);
            dto.setU90(u90);
            dto.setL90((long) (((double) u90 / o90) * 100) + "%");
            res.add(dto);
        });
        return R.ok(res);
    }

    @ApiOperation(value = "中奖概率修改")
    @PostMapping("/prizeUpdate")
    public Object prizeUpdate(@RequestBody Req2 req) {
        TPrize tPrize = prizeMapper.selectByPrimaryKey(req.getId());
        tPrize.setChance(req.getChance());
        prizeMapper.updateByPrimaryKey(tPrize);
        return R.ok();
    }

    @Autowired
    private TPrizeMapper prizeMapper;
    @Autowired
    private TPrizeRecordMapper prizeRecordMapper;
}
